home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageioproc.lib / dev / examples / inlineonbuffer / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-17  |  5.0 KB  |  207 lines

  1. /* This example use of imageprocess.library loads the image file specified
  2.         on the command line, processes it inline in the imageio handle and
  3.         views it halved in size in a guigfx window.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19.  
  20. #include <imageio/imageio.h>
  21. #include <imageio/imageio_protos.h>
  22. #include <imageio/imageio_pragmas.h>
  23.  
  24. #include <imageprocess/imageprocess.h>
  25. #include <imageprocess/imageprocess_protos.h>
  26. #include <imageprocess/imageprocess_pragmas.h>
  27.  
  28. #include <guigfx/guigfx.h>
  29. #include <pragmas/guigfx_pragmas.h>
  30. #include <guigfx/guigfx_protos.h>
  31.  
  32. /* Function prototypes */
  33. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  34. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  35.  
  36. extern struct Library *DOSBase;
  37. struct Library *ImageIOBase, *IntuitionBase, *ImageProcessBase;
  38.  
  39. void main( int argc, char **argv )
  40. {
  41.     if ( argv[1] )
  42.     {
  43.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  44.         ImageProcessBase = OpenLibrary( "imageprocess.library", 1 );
  45.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  46.         if ( IntuitionBase && ImageIOBase && ImageProcessBase )
  47.         {
  48.             struct ImageHandle *ih;
  49.             ULONG err;
  50.  
  51.             err = AllocImage( &ih,
  52.                 IMG_SrcFilename, argv[1],
  53.                 TAG_DONE );
  54.             if ( !err )
  55.             {
  56.                 ULONG num = 1, denom = 2;
  57.                 ULONG x, y, bpp;
  58.                 UBYTE *buffer, cs;
  59.  
  60.                 err = GetImageAttrs( ih,
  61.                     IMG_Width, &x,
  62.                     IMG_Height,&y,
  63.                     IMG_BytesPerPixel, &bpp,
  64.                     IMG_ColourSpace, &cs,
  65.                     IMG_TestScaleNum, num,
  66.                     IMG_TestScaleDenom, denom,
  67.                     TAG_DONE );
  68.                 if ( !err )
  69.                 {
  70.                     printf( "width=%ld, height=%ld\n", x, y );
  71.                     printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  72.  
  73.                     err = ReadImage( ih,
  74.                         IMG_ScaleNum, num,
  75.                         IMG_ScaleDenom, denom,
  76.                         IMG_ImageBuffer, &buffer,
  77.                         IMG_ProgressHook, progressFunc,
  78.                         TAG_DONE );
  79.                     if ( !err )
  80.                     {
  81.                         ULONG iperr;
  82.  
  83.                         iperr = DoImageProcess( IMP_Process_AutoGamma,
  84.                             IMP_SourceBuffer, buffer,
  85.                             IMP_SourceWidth, x,
  86.                             IMP_SourceHeight, y,
  87.                             IMP_SourceBytesPerPixel, bpp,
  88.                             IMP_SourceColourSpace, cs,
  89.                             IMP_ProcessInline, TRUE,
  90.                             TAG_DONE );
  91.                         if ( !iperr )
  92.                         {
  93.                             printf("%ld x %ld\n",x,y);
  94.  
  95.                             DisplayGuiGfx( buffer, cs, bpp, x, y );
  96.                         }
  97.                         else printf( "doimageprocess error:%ld\n", iperr );
  98.                     }
  99.                     else printf( "read image error:%d\n", err );
  100.                 }
  101.                 else printf( "get image attrs error:%d\n", err );
  102.  
  103.                 FreeImage( ih );
  104.             }
  105.             else printf( "alloc image error:%d\n", err );
  106.         }
  107.  
  108.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  109.         if ( ImageProcessBase ) CloseLibrary( ImageProcessBase );
  110.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  111.     }
  112. }
  113.  
  114. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  115. {
  116.     static int prevpercent = 0;
  117.  
  118.     int percent = ( curr * 100 ) / lines;
  119.  
  120.     if ( prevpercent != percent )
  121.     {
  122.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  123.     }
  124.  
  125.     prevpercent = percent;
  126.  
  127.     return NULL;
  128. }
  129.  
  130. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  131. {
  132.     struct Library *GuiGFXBase;
  133.  
  134.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  135.     if ( GuiGFXBase )
  136.     {
  137.         struct Window *win;
  138.         APTR dh, pi;
  139.         UBYTE *argb;
  140.  
  141.         win = OpenWindowTags( NULL,
  142.             WA_Title, "Proof",
  143.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  144.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  145.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  146.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  147.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  148.             WA_Left, 16,
  149.             WA_Top, 16,
  150.             WA_Width, x,
  151.             WA_Height, y,
  152.             TAG_DONE );
  153.  
  154.         if ( win != NULL )
  155.         {
  156.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  157.  
  158.             if ( dh )
  159.             {
  160.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  161.  
  162.                 if ( argb )
  163.                 {
  164.                     int i, count = 0;
  165.                     char **dest;
  166.  
  167.                     /* Convert an RGB buffer to an ARGB buffer setting A to 0.*/
  168.                     dest = (char **)argb;
  169.  
  170.                     for ( i = 0; i < x * y * 3; i += 3 )
  171.                     {
  172.                         dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  173.                     }
  174.  
  175.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  176.  
  177.                     if ( pi )
  178.                     {
  179.                         struct Message *msg;
  180.  
  181.                         DrawPicture( dh, pi, 0, 0, NULL );
  182.  
  183.                         Wait( 1L << win->UserPort->mp_SigBit );
  184.  
  185.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  186.  
  187.                         DeletePicture( pi );
  188.                     }
  189.                     else printf( "failed to create picture\n" );
  190.  
  191.                     FreeVec( argb );
  192.                 }
  193.                 else printf( "failed to allocate argb buffer\n" );
  194.  
  195.                 ReleaseDrawHandle( dh );
  196.             }
  197.             else printf( "failed to get drawhandle\n" );
  198.  
  199.             CloseWindow( win );
  200.         }
  201.         else printf( "failed to open window\n" );
  202.     }
  203.     else printf( "failed to open guigfx.library\n" );
  204.  
  205.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  206. }
  207.